package day9.guanlixitong.src.ui;
/*
public class test {
}*/
import day9.guanlixitong.src.bean.User;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
//监听点击登录按钮并且给出反应，肯定是别人封装好的
public class LoginFrame extends JFrame implements ActionListener {

    private JTextField usernameField;//用户名输入框
    private JPasswordField passwordField;//密码输入框

    private JButton loginButton;//登录按钮
    private JButton registerButton;//注册按钮
     static ArrayList<User> all_users=new ArrayList<>();


    static//静态代码块，加载这个类的时候也会加载他（只加载一次）
    {
        all_users.add(new User("小明","admin","123"));
        all_users.add(new User("小王","admin","123"));
        all_users.add(new User("小李","admin","123"));
    }

    public static void main(String[] args) {
        // 确保Swing组件在事件调度线程上创建
        SwingUtilities.invokeLater(() -> new LoginFrame());
    }
    public LoginFrame() {
        setTitle("登录窗口");
        setSize(400, 220); // 稍微增加窗口高度以适应新的布局
        setLocationRelativeTo(null); // 居中显示
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // 设置内容面板的布局为BorderLayout
        setLayout(new BorderLayout());

        // 创建一个面板来放置登录表单
        JPanel loginPanel = new JPanel();
        loginPanel.setLayout(new GridLayout(3, 2, 10, 10));
        loginPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); // 内边距

        // 添加用户名标签和文本框
        JLabel userLabel = new JLabel("用户名:", SwingConstants.RIGHT);
        userLabel.setFont(new Font("楷体", Font.PLAIN, 14));
        loginPanel.add(userLabel);

        usernameField = new JTextField();
        usernameField.setFont(new Font("楷体", Font.PLAIN, 14));
        loginPanel.add(usernameField);

        // 添加密码标签和密码框
       JLabel passLabel = new JLabel("密码:", SwingConstants.RIGHT);
        passLabel.setFont(new Font("楷体", Font.PLAIN, 14));
        loginPanel.add(passLabel);

        passwordField = new JPasswordField();
        passwordField.setFont(new Font("楷体", Font.PLAIN, 14));
        passwordField.setEchoChar('*');
        loginPanel.add(passwordField);

        // 创建按钮面板
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new GridLayout(1, 2, 10, 10)); // 一行两列布局

        // 添加登录按钮
         loginButton = createStyledButton("登录");
        buttonPanel.add(loginButton);
        loginButton.addActionListener(this);
        //给登录按钮添加一个监听属性，然后需要一个监听器对象，这个窗口本身就是，就添加呗

        // 添加注册按钮
        registerButton = createStyledButton("注册");
        buttonPanel.add(registerButton);
        registerButton.addActionListener(this);

        // 将登录表单和按钮添加到主窗口
        add(loginPanel, BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);

        // 设置背景颜色
        getContentPane().setBackground(new Color(240, 240, 240));

        // 设置可见性
        setVisible(true);
    }


    @Override
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource()==loginButton)
        {
           // System.out.println("登录开始");
                 login();
        }
        else
        {
            System.out.println("注册开始");
//            JFrame J=new JFrame();
//            J.setTitle("注册窗口");
//            J.setSize(400, 220); // 稍微增加窗口高度以适应新的布局
//            J.setLocationRelativeTo(null); // 居中显示
//            J.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//            J.setVisible(true);
            register();
        }
    }


    private JButton createStyledButton(String text) {
        JButton button = new JButton(text);
        button.setFont(new Font("楷体", Font.BOLD, 14));
        button.setBackground(new Color(0, 128, 255));
        button.setForeground(Color.WHITE);
        button.setFocusPainted(false);
        button.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
        button.setPreferredSize(new Dimension(120, 30)); // 设置按钮大小
        return button;
    }

    public void login()
    {
        String username=usernameField.getText();
        //String password=(String)passwordField.getPassword();不能直接char[]转化成String必须new String采用String的构造
        String password=new String(passwordField.getPassword());

        if(getUserByLoginName(username,password)!=null)
        {
            JOptionPane.showMessageDialog(this,"登陆成功");
            //EmployeeManager E=new EmployeeManager();
            new EmployeeManager(username);
            this.dispose();//登陆界面消失
        }
        else JOptionPane.showMessageDialog(this,"用户名或者密码错误");
    }
    public User getUserByLoginName( String username,String password)
    {

        for(int i=0;i<all_users.size();i++)
        {
            if(all_users.get(i).getLoginname().equals(username)&&all_users.get(i).getPassword().equals(password))
            {
                return all_users.get(i);
            }
        }
        return null;
    }
    public void register()
    {
        new RegisterFrame();

    }

}

